package com.heaton.videocar.ui

import android.util.Log
import cn.com.heaton.blelibrary.ble.model.BleDevice
import cn.com.heaton.blelibrary.ble.utils.ByteUtils
import com.heaton.baselib.app.Navigation
import com.heaton.baselib.base.BaseNavActivity
import com.heaton.videocar.R
import com.heaton.videocar.core.BleManager
import kotlinx.android.synthetic.main.activity_control.*
import kotlinx.android.synthetic.main.fragment_cool.*
import kotlinx.android.synthetic.main.fragment_hot.*

class ControlActivity: BaseNavActivity() {
    override fun layoutId() = R.layout.activity_control
    override fun containerViewId() = R.id.container
    override fun mode(): Navigation.MODE {
        return Navigation.MODE.SHOW
    }

    private val coolFragment = CoolFragment()
    private val hotFragment = HotFragment()
    private var isCoolShow = true
    private var isCharging = false//是否正在充电
    private var currentBattery = 100

    override fun bindData() {
        setTimeout(1200){
            //获取设备电量
            BleManager.get().getDeviceBattery()
        }
        Navigation.get().navigate(coolFragment)
        BleManager.get().setBleCallback(this, object :BleManager.BleCallback(){
            override fun onChanged(device: BleDevice?, data: ByteArray) {
                Log.e(TAG, "onChanged：${ByteUtils.toHexString(data)}")
                if (data[1] == 0x53.toByte()) {//电量
                    currentBattery = data[2].toInt()
                    if (!isCharging){
                        updateBatteryUI(currentBattery)
                    }
                }else if (data[1] == 0x51.toByte()){//状态： 运行状态（初始化(0X00)，运行(0X01)，充电中(0X02)，ota升级(0X03)）
                    when(data[2].toInt()){
                        0x02->{
                            isCharging = true
                            currentBattery = data[3].toInt()
                            if (currentBattery != 100){
                                iv_battery.setImageResource(R.mipmap.icon_batterying)
                            }else{
                                iv_battery.setImageResource(R.mipmap.icon_battery_full)
                            }
                        }
                    }
                }else if (data[1] == 0x56.toByte()){//切换制冷制热模式
                    val level = data[2]
                    if (level in 0..4){//制冷
                        showCool()
                    }else{//制热
                        showHot()
                    }
                }
            }

            override fun onConnectionChanged(device: BleDevice) {
                if (device.isDisconnected){
                    isCharging = false
                    updateBatteryUI(currentBattery)
                }
            }
        })
    }

    private fun updateBatteryUI(value: Int){
        when(value){
            in 0..20->iv_battery.setImageResource(R.mipmap.icon_battery_01)
            in 20..40->iv_battery.setImageResource(R.mipmap.icon_battery_02)
            in 40..60->iv_battery.setImageResource(R.mipmap.icon_battery_03)
            in 60..80->iv_battery.setImageResource(R.mipmap.icon_battery_04)
            else->iv_battery.setImageResource(R.mipmap.icon_battery_05)
        }
    }

    override fun bindListener() {
        super.bindListener()
        iv_cool.setOnClickListener {
            if (isCoolShow)return@setOnClickListener
            hotFragment.resetLevel()
            BleManager.get().controlMode(0, null)
            showCool()
        }
        iv_hot.setOnClickListener {
            if (!isCoolShow)return@setOnClickListener
            //复位
            coolFragment.resetLevel()
            BleManager.get().controlMode(16, null)
            showHot()
        }
        iv_settings.setOnClickListener {
            toActivity(SettingsActivity::class.java)
        }
    }

    private fun showCool(){
        isCoolShow = true
        iv_cool.setImageResource(R.mipmap.icon_snowflake_selected)
        iv_hot.setImageResource(R.mipmap.icon_sun)
        Navigation.get().navigate(coolFragment)
    }

    private fun showHot(){
        isCoolShow = false
        iv_hot.setImageResource(R.mipmap.icon_sun_selected)
        iv_cool.setImageResource(R.mipmap.icon_snowflake)
        Navigation.get().navigate(hotFragment)
    }

    override fun finish() {
        super.finish()
        BleManager.get().disconnect()
    }

}